package com.pignest.hotel.ui.reception;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Checkbox;

import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.LivingSpecifics;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.OperateState;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.definition.RoomUseStatus;

import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;

import com.pignest.hotel.util.UIUtils;

public class ChangeRoomWindow extends Window implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -448570245636189214L;
	/**
	 * 
	 */
	

	private ReceptionService receptionService;
	private MemberService memberService;
	private RoomService roomService;
	private Room room;
	private Room selectRoom;//选择的房间
	private int adjustPrice;//不管是会员还是非会员，最后的房价
	
	public Living living;
	private boolean success;//业务是否成功完成
	
	private Label roomNameLabel;			//房间名
	//private Label livingTypeLabel;//入住类型
	private Label roomTypeLabel;//房间类型
	private Label roomStatusLabel;//当前房间状态
	private Textbox relativeRoomTextbox;		//关联的房间号, 团队, 联房
    private Listbox roomTypeList; //房间类型
	private Listbox liveListbox;
	
	public Listbox selectRoomListbox;
	private Textbox newroomName;
	private Label currentPrice;
	private Label memberPrice;
	private Label currentRoomStatus;
	
	private Textbox offsetPrice;
    private Checkbox isOffset;
    private Label changeTip;
    private Listbox changeReason;
	
	private Listbox LivingHistoryListbox;
	public ChangeRoomWindow() {
		receptionService = Services.getReceptionService();
		memberService =Services.getMemberService();
		roomService =Services.getRoomService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			roomNameLabel = (Label) getFellow("roomName");

			roomTypeLabel =(Label)getFellow("roomType");
			roomStatusLabel=(Label)getFellow("roomStatus");
			relativeRoomTextbox = (Textbox) getFellow("relativeRoom");
			roomTypeList=(Listbox)getFellow("roomTypeList");
			liveListbox=(Listbox)getFellow("liveListbox");
			LivingHistoryListbox=(Listbox)getFellow("LivingHistoryListbox");
			selectRoomListbox=(Listbox)getFellow("selectRoomListbox");
			
			currentPrice=(Label)getFellow("currentPrice");
			memberPrice=(Label)getFellow("memberPrice");
			currentRoomStatus=(Label)getFellow("currentRoomStatus");
			newroomName=(Textbox)getFellow("newroomName");
			
			offsetPrice=(Textbox)getFellow("offsetPrice");
			isOffset =(Checkbox)getFellow("isOffset");
			changeTip=(Label)getFellow("changeTip");
			changeReason=(Listbox)getFellow("changeReason");
			Living l= (Living)receptionService.getLiving(room);			
			this.living=l;
			flush();
			
		}
	}
	public void queryLiveList(){

		if(this.living!=null){
			this.liveListbox.getItems().clear();
			Listitem listitem = new Listitem();
			listitem.setParent(liveListbox);
			listitem.appendChild(new Listcell(room.getName()));//房间名
			listitem.appendChild(new Listcell(room.getType().getName()));//房类
			listitem.appendChild(new Listcell(this.living.getCustormName()));//姓名
			
			listitem.appendChild(new Listcell(LivingState.valueOf(this.living.getLiveState()).getDisplayName()));//入住状态
			listitem.appendChild(new Listcell(this.living.getLiveType()));//入住类型
			listitem.appendChild(new Listcell(DateUtil.toString(this.living.getLiveTime())));//到达时间
			listitem.appendChild(new Listcell(DateUtil.toString(this.living.getExpectLeaveDate())));//预定离开时间
			listitem.appendChild(new Listcell(Utils.toPrice(this.living.getAvailable())));//余额
			listitem.appendChild(new Listcell(this.living.getRemark()));//备注
			
		}
	}
	public void queryLiveHisory(){
		if(this.living!=null){
			this.LivingHistoryListbox.getItems().clear();
			StringBuffer hql=new StringBuffer();
			hql.append(" from LivingSpecifics where living.id='"+this.living.getId()+"'");
			List <LivingSpecifics> list=receptionService.find(hql.toString());
			for( LivingSpecifics s:list){
			
			Listitem listitem = new Listitem();
			listitem.setParent(LivingHistoryListbox);
			listitem.appendChild(new Listcell(s.getRoom()));//房间名
			listitem.appendChild(new Listcell(s.getOperateType()));//操作类型
			listitem.appendChild(new Listcell(Utils.toPrice(s.getRoomPrice())));//房价
			listitem.appendChild(new Listcell(Utils.toPrice(s.getPay())));//金额
			//listitem.appendChild(new Listcell(DateUtil.toString(s.getGrantDate())));//授权时间"
			listitem.appendChild(new Listcell(DateUtil.toString(s.getLeaveDate())));//离开时间
			listitem.appendChild(new Listcell(s.getRemark()));//备注
			}
			
		}
	}
    public void setBaseMessageValue(){
    	roomNameLabel.setValue(room.getName());
    	roomTypeLabel.setValue(room.getType().getName());
    	roomStatusLabel.setValue(RoomLiveStatus.valueof(room.getLiveStatus()).getName()+RoomUseStatus.valueof(room.getUseStatus()).getName());
    	
    }
	/**
	 * 操作完成相关业务后调用
	 * */
	public void flush(){
		setBaseMessageValue();
		queryLiveList();
		queryLiveHisory();
		querySelectRoom();

	}
    public String getRoomTypeId(){
    	String typeId=UIUtils.getSelectedValue(roomTypeList);
    	return typeId;
    }
	

	public void setRoom(Room room) {
		this.room = room;
	}
	
	
    public boolean isSuccess() {
		return success;
	}
    public int getMemberPrice(String MemberId,String roomId){
    	return memberService.getMemberPrice(MemberId, roomId);
    }
    public void querySelectRoom(){
    	selectRoomListbox.getItems().clear();
    	String roomTypeId=UIUtils.getSelectedValue(roomTypeList);
		RoomType roomType =roomService.getRoomType(roomTypeId);
		List <Room>l=roomService.listRooms(RoomLiveStatus.EMPTY,roomType);
		for(final Room r:l){
			Listitem listitem = new Listitem();
			listitem.setParent(selectRoomListbox);
			listitem.appendChild(new Listcell(r.getName()));//房间名
			listitem.appendChild(new Listcell(r.getType().getName()));//房型
			//RoomStatus
			listitem.appendChild(new Listcell(RoomLiveStatus.valueof(r.getLiveStatus()).getName()+RoomUseStatus.valueof(r.getUseStatus()).getName()));//状态
			listitem.appendChild(new Listcell(r.getFloor()));//楼层
			listitem.appendChild(new Listcell(r.getBuilding()));//分栋
			listitem.appendChild(new Listcell(r.getRepairStatus()));//维修状态
			listitem.appendChild(new Listcell("是"));//是否可以入住
			listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				public void onEvent(Event event) throws Exception { 
					setSelectRoom(r);
					setAfterSelectRoom();
				}
			});
			
		}
	}
    public void setAfterSelectRoom(){
    	
    	if(selectRoom!=null){
    		newroomName.setValue(selectRoom.getName());
    		currentPrice.setValue(Utils.toPrice(selectRoom.getType().getPrice()));
    		currentRoomStatus.setValue(RoomLiveStatus.valueof(selectRoom.getLiveStatus()).getName()+RoomUseStatus.valueof(selectRoom.getUseStatus()).getName());
    		if(this.living.getMember()!=null){
    			int i=getMemberPrice(this.living.getMember().getId(),selectRoom.getType().getId());
    			memberPrice.setValue(Utils.toPrice(i));
    			adjustPrice=i;
    			offsetPrice(i);
    		}else{
    			offsetPrice(selectRoom.getType().getPrice());
    			adjustPrice=selectRoom.getType().getPrice();
    		}
    	}
    }
    public void offsetPrice(int curprice){
    	int oPrice =this.living.getAdjustPrice();
    	int offset=0;
    	if(curprice<oPrice)
    	{
    		 offset= oPrice-curprice;
    		 isOffset.setChecked(true);
    		 offsetPrice.setValue(Utils.toPrice(offset));
    		 changeTip.setValue("建议补偿差价："+Utils.toPrice(offset));
    	}else
    	{
    		isOffset.setChecked(false);
    		offsetPrice.setValue("0");
    		changeTip.setValue("");
    	}
    	
    }
    public String change(){
    	try{
    		
    		StringBuffer msg =new StringBuffer();
    		msg.append("你好：").append(living.getCustormName()).append("\n");
    		String sChangeReason=UIUtils.getSelectedValue(changeReason);
    		LivingSpecifics livingLs=receptionService.querySpec(living);
    		livingLs.setOperateType(OperateState.CHANGEROOM.name());
    		livingLs.setRemark("由"+livingLs.getRoom()+"换成"+selectRoom.getName());
    		livingLs.setLeaveDate(new Date());
    		LivingSpecifics ls = new LivingSpecifics();
    		ls.setLiving(living);
    		ls.setOperateDate(new Date());
    		//ls.setOperateType("换房");
    		ls.setOperateType(OperateState.LIVING.name());
    		ls.setRoom(selectRoom.getName());
    		ls.setRoomPrice(adjustPrice);
    		
    		ls.setRemark(sChangeReason);
    		ls.setLeaveDate(this.living.getLeaveDate());
    		ls.setGrantDate(this.living.getGrantDate());
    		TbAccounts ta = null;
    		String offsetMsg="";
    		if(isOffset.isChecked()){
    			
    			ta = new TbAccounts();
    			ta.setLiving(living);
    			ta.setCounts(1);
    			ta.setName(InComeItemType.FJTZF.getValue());
    			int iOffsetPrice =Utils.toPrice(offsetPrice.getValue());
    			ls.setPay(iOffsetPrice);
    			ta.setPrice(-iOffsetPrice);
    			ls.setPay(iOffsetPrice);
    			//ls.setRemark(InComeItemType.FJTZF.getValue()+",调整费:"+Utils.toPrice(iOffsetPrice));
    			ta.setOptime(new Date());;
    			ta.setOpUser(Hotel.getCurrentUser().getName());
    			ta.setType(InComeItemType.FJTZF.name());
    			ta.setEndstate("否");
    			ta.setOpdate(DateUtil.currentDateToString(DateUtil.DEFAULT_DATE_FORMAT));
    			ta.setPrice2(iOffsetPrice);
    			ta.setRemark("由"+livingLs.getRoom()+"换成"+selectRoom.getName()+"调整费："+Utils.toPrice(iOffsetPrice));
    			offsetMsg="补偿差价："+Utils.toPrice(iOffsetPrice);
    		}
    		msg.append("是否由").append(this.living.getRoom().getName()).append("\n");
	    	msg.append("换成").append(this.selectRoom.getName()).append("\n");
	    	msg.append("当前的房价为:").append(Utils.toPrice(adjustPrice)).append("\n");
	    	msg.append(offsetMsg);
	    	
	    	if(UIUtils.confirm(msg.toString())){
	    		this.living.setRoom(selectRoom);
	    		this.living.setAdjustPrice(adjustPrice);
	    		this.living.setRoomPrice(selectRoom.getType().getPrice());
	    		room.setUseStatus(RoomUseStatus.DIRTY.name());
	    		room.setLiveStatus(RoomLiveStatus.EMPTY.name());
	    		selectRoom.setLiveStatus(RoomLiveStatus.FILLED.name());
	    		receptionService.changeRoom(ls,livingLs, this.living, ta,room,selectRoom);
	    		UIUtils.alert("请给原房卡重新授权!");
	    		living =receptionService.get(Living.class, this.living.getId());
	    		this.setRoom(selectRoom);
	    		flush();
	    		success=true;
	    		return null;
	    	}else{
	    		return "取消！";
	    	}
    	}catch(Exception e){
    		e.printStackTrace();
    		return e.getMessage();
    	}
    }
    public Room getSelectRoom() {
		return selectRoom;
	}
	public void setSelectRoom(Room selectRoom) {
		this.selectRoom = selectRoom;
	}
	public Living getLiving() {
		return living;
	}
	public void setLiving(Living living) {
		this.living = living;
	} 
	
}
